#include "stdafx.h"
#include "LoginSrvNet.h"
#include "Utils\Debug.h"

void LoginSrvNet::onAccept(EssAcceptor* acceptor, EssConnector* connector)
{
	char* ip;
	u_short port;
	if (connector->getPeerName(&ip, &port))
	{
		char log[MAX_PATH] = {0};
		sprintf_s(log, "Accept a new client with IP %s, Port %d.\n", ip, port);
		mLog += log;
	}
	//char retBuf[1024] = {0};
	//sprintf_s(retBuf, "HELLO FLASH\0");
	//connector->sendSock(retBuf, strlen(retBuf));
}

bool LoginSrvNet::onReceive(EssConnector* connector)
{
	char* ip;
	u_short port;
	if (connector->getPeerName(&ip, &port))
	{
		char log[MAX_PATH] = {0};
		sprintf_s(log, "Recv Msg From Client IP %s, Port %d.\n", ip, port);
		mLog += log;
	}
	/*
	char recvBuf[4096] = {0};//char recvBuf[MAX_PATH] = {0};
	sprintf_s(recvBuf, "Recv Msg is %s\n\0", connector->getRecvBuffer().base());
	mLog += recvBuf;
	char retBuf[64] = {0};
	sprintf_s(retBuf, "#NICE TO MEET U TOO CLIENT#\n");
	connector->sendSock(retBuf, strlen(retBuf));
	*/
	BYTE* bytes = (BYTE*)connector->getRecvBuffer().base();
	int cmdLen = int(*bytes);
	TRACE("cmdLen=%d\n", cmdLen);
	bytes += 4;
	bytes ++;
	bytes ++;
	BYTE mainCmd = *bytes;
	bytes ++;
	BYTE subCmd = *bytes;
	bytes ++;
	const char* account = (char*)bytes;
	TRACE("account=%s\n", account);
	int accountLen = strlen(account);
	bytes += accountLen+1;
	const char* pwd = (char*)bytes;
	TRACE("pwd=%s\n", pwd);
	int pwdLen = strlen(pwd);
	return true;
}

void LoginSrvNet::onDisconnect(EssConnector* connector)
{
	char* ip;
	u_short port;
	if (connector->getPeerName(&ip, &port))
	{
		char log[MAX_PATH] = {0};
		sprintf_s(log, "A client disconnect with IP %s, Port %d.\n", ip, port);
		mLog += log;
	}
	removeConnector(connector);
}

void LoginSrvNet::broadcast(const char* buf, int len)
{
	for (ConnectorIterator c = mConnectors.begin(); c != mConnectors.end(); ++c)
	{
		c->second->sendSock(buf, len);
	}
}